#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/types.h> 
#include <stdlib.h>
#include <stdint.h>

#include "cgic.h"
#include "sem.h"

typedef struct 
{
	uint8_t head[3];	 //标识位st:
	uint8_t type;		 //数据类型
	uint8_t snum;		 //仓库编号
	uint8_t temp[2];	 //温度	
	uint8_t hum[2];		 //湿度
	uint8_t x;			 //三轴信息
	uint8_t y;			 
	uint8_t z;			 
	uint32_t ill;		 //光照
	uint32_t bet;		 //电池电量
	uint32_t adc; 		 //电位器信息

}sendEnv;

#define N 1024

int cgiMain()
{
	key_t key;
	int semid;
	int shmid;
	sendEnv *pmyEnv = NULL;
	sendEnv myEnv;

	if((key = ftok("/", 'a')) < 0)
	{
		perror("ftok");
		exit(1);
	}
	
    shmid = shmget(key, sizeof(sendEnv), IPC_CREAT | IPC_EXCL | 0777);//创建共享内存
    if (shmid < 0)
    {
        if (errno == EEXIST)
        {
            shmid = shmget(key, 0, 0777);
        }
        else
        {
            perror("fail to shmget");
            exit(-1);
        }
    }
	
	if((pmyEnv = (sendEnv *)shmat(shmid, NULL, 0)) == (void*)-1)
	{
		perror("shmat");
		exit(1);
	}

//	sem_p(semid,0);
	myEnv = *pmyEnv;
//	sem_v (semid, 0);

	cgiHeaderContentType("text/html"); 
	fprintf(cgiOut, "<HTML><HEAD>"); 
	fprintf(cgiOut, "<TITLE>当前实时数据</TITLE>");  
	fprintf(cgiOut, "<style type=\"text/css\"> body,td,th {font-size: 12px;margin-left: 0px;margin-top: 0px;}</style></HEAD>"); 
	fprintf(cgiOut, "<BODY BGCOLOR=\"#F8F9FA\">"); 

	fprintf(cgiOut, "<meta http-equiv=\"refresh\" content=\"1\">");

	fprintf(cgiOut, "<table width=\"100%%\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\">");
	// 实时数据表头
	fprintf(cgiOut, 	"<tr bgcolor=\"#DEE4E6\">");
	
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">名称</td>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">最新数值</td>");
	fprintf(cgiOut, 	"</tr>");
	
	// 温度采集	
	fprintf(cgiOut, 	"<tr bgcolor=\"#f2f2f2\">");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">环境温度</td>");
	fprintf(cgiOut, 	"<td height=\"30\" align=\"center\" valign=\"middle\">%d℃</td>", myEnv.temp[0]);
	fprintf(cgiOut, 	"</tr>");
	
	// 湿度采集
	fprintf(cgiOut, 	"<tr>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">环境湿度</td>");
	fprintf(cgiOut, 	"<td height=\"30\" align=\"center\" valign=\"middle\">%d％</td>", myEnv.hum[0]);
	fprintf(cgiOut, 	"</tr>");
	
	// 光照采集
	fprintf(cgiOut, 	"<tr bgcolor=\"#f2f2f2\">");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">环境光照</td>");
	fprintf(cgiOut, 	"<td height=\"30\" align=\"center\" valign=\"middle\">%dlux</td>", myEnv.ill);
	fprintf(cgiOut, 	"</tr>");
	
	// 电量采集
	fprintf(cgiOut, 	"<tr>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">电池电量</td>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">%dV</td>", myEnv.bet);
	fprintf(cgiOut, 	"</tr>");
	
	// ADC采集	
	fprintf(cgiOut, 	"<tr bgcolor=\"#f2f2f2\">");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">模拟电压</td>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">%dV</td>", myEnv.adc);
	fprintf(cgiOut, 	"</tr>");
	
	fprintf(cgiOut, 	"<tr>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">设备姿态</td>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\"><table width=\"200\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">");
	fprintf(cgiOut, 			"<tr align=\"center\" valign=\"middle\">");
	fprintf(cgiOut, 					"<td width=\"25%%\">&nbsp;</td>");
	fprintf(cgiOut, 					"<td width=\"25%%\">X</td>");
	fprintf(cgiOut, 					"<td width=\"25%%\">Y</td>");
	fprintf(cgiOut, 					"<td width=\"25%%\">Z</td>");
	fprintf(cgiOut, 			"</tr>");
	fprintf(cgiOut, 			"<tr align=\"center\" valign=\"middle\">");
	fprintf(cgiOut, 					"<td>原始值</td>");
	fprintf(cgiOut, 					"<td>%d</td>", myEnv.x);
	fprintf(cgiOut, 					"<td>%d</td>", myEnv.y);
	fprintf(cgiOut, 					"<td>%d</td>", myEnv.z);
	fprintf(cgiOut, 			"</tr>");
	fprintf(cgiOut, 		"</table></td>");
	fprintf(cgiOut, 	"</tr>");
	fprintf(cgiOut, "</table>");

	fprintf(cgiOut, "<script type=\"text/jscript\">");
	fprintf(cgiOut, "setTimeout(\"self.close()\", 3000)");
	fprintf(cgiOut, "</script>");
	fprintf(cgiOut, "</BODY>"); 
	fprintf(cgiOut, "</HTML>");
	fflush(stdout);

	return 0;
}





